package drinkreos;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import drinkreos.model.Card;
import drinkreos.model.Card.CardType;
import drinkreos.model.Player;

public class GUI {
	
	private static final Font font=new Font("Arial", Font.PLAIN, 40);
	private static final Font smallFont=new Font("Arial", Font.PLAIN, 30);
	private JList<Player> playerList;
	private final JList<Card> handCardListComponent;
	private final DefaultListModel<Card> handCardList;
	private final JPanel activeCardPane;
	private CardView selectedHandCardView;
	private JFrame frame;
	private Game game;
	
	public GUI(JFrame frame, final Game game) {
		this.frame=frame;
		this.game=game;
		frame.getContentPane().setLayout(new BorderLayout());
		
		final JPanel playerPanel=new JPanel();
			frame.getContentPane().add(playerPanel,BorderLayout.LINE_START);
			playerList=new JList<Player>(game.getPlayers().toArray(new Player[game.getPlayers().size()]));
				playerPanel.add(playerList);
				playerList.setEnabled(false);
				playerList.setSelectedIndex(0);
				playerList.setFont(font);
		
		activeCardPane=new JPanel();
			activeCardPane.setLayout(new WrapLayout());
		JScrollPane scrollPane=new JScrollPane(activeCardPane,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			frame.getContentPane().add(scrollPane,BorderLayout.CENTER);
			
			
		final JPanel rightPanel=new JPanel();
			frame.getContentPane().add(rightPanel,BorderLayout.LINE_END);
			rightPanel.setLayout(new BorderLayout());
			handCardListComponent=new JList<Card>();
				handCardList=new DefaultListModel<Card>();
				handCardListComponent.setModel(handCardList);
				handCardListComponent.setFont(smallFont);
				rightPanel.add(handCardListComponent,BorderLayout.BEFORE_FIRST_LINE);
				handCardListComponent.addListSelectionListener(new ListSelectionListener() {	
					public void valueChanged(ListSelectionEvent e) {
						selectedHandCardView.setCard((Card) handCardListComponent.getSelectedValue());
					}});
			
			selectedHandCardView=new CardView();
				rightPanel.add(selectedHandCardView,BorderLayout.CENTER);
				
			final JPanel rightBottomPanel=new JPanel();
				rightBottomPanel.setLayout(new BoxLayout(rightBottomPanel, BoxLayout.Y_AXIS));
				rightPanel.add(rightBottomPanel,BorderLayout.AFTER_LAST_LINE);
				JButton playHandCardButton=new JButton("Play Card");
					rightBottomPanel.add(playHandCardButton);
					playHandCardButton.setFont(font);
					playHandCardButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							if(handCardList.getSize()>0) {
								Card c=(Card) handCardListComponent.getSelectedValue();
								game.getActivePlayer().getActiveCards().remove(c);
								handCardList.removeElement(c);
								showCard(c);
							}
						}});
				JButton drawCardButton=new JButton("Draw Card");
					rightBottomPanel.add(drawCardButton);
					drawCardButton.setFont(font);
					drawCardButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							
							Card c=game.drawCard(game.getActivePlayer());
							if(c.getType()==CardType.USABLE) {
								game.getActivePlayer().getHandCards().add(c);
								handCardList.addElement(c);
							}
							else {
								Player target=showCard(c);
								if(c.getType()==CardType.PERMANENT || c.getType()==CardType.SEMIPERMANENT) {
									if(target.equals(game.getActivePlayer())) {
										addCardToActiveCardPane(c);
										target.getActiveCards().add(c);
									}
									else
										target.getActiveCards().add(c);
								}
							}
						}});
				JButton nextPlayerButton=new JButton("Next Player");
					rightBottomPanel.add(nextPlayerButton);
					nextPlayerButton.setFont(font);
					nextPlayerButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							game.nextPlayer();
						}});
					
	}

	public void setActivePlayer(Player p) {
		selectedHandCardView.setCard(null);
		playerList.setSelectedValue(p, true);
		
		handCardList.removeAllElements();
		for(Card c:p.getHandCards())
			handCardList.addElement(c);
		
		activeCardPane.removeAll();
		for(Card c:p.getActiveCards()) {
			addCardToActiveCardPane(c);
		}
		activeCardPane.updateUI();
	}
	
	private void addCardToActiveCardPane(final Card c) {
		final CardView cv=new CardView();
		cv.enableInputMethods(true);
		cv.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent event) {
				if (event.getClickCount() == 2) {
					activeCardPane.remove(cv);
					game.getActivePlayer().getActiveCards().remove(c);
					activeCardPane.updateUI();
				}
			}
		});
		activeCardPane.add(cv);
		cv.setCard(c);
		activeCardPane.updateUI();
	}

	public Player showCard(Card c) {
		JPanel panel=new JPanel();
			JList<Player> list=new JList<Player>(game.getPlayers().toArray(new Player[game.getPlayers().size()]));
				panel.add(list);
				list.setSelectedValue(game.getActivePlayer(),true);
				list.setFont(font);
			CardView cv=new CardView();
				cv.setCard(c);
				panel.add(cv);
			
		
		JOptionPane.showMessageDialog(frame, panel,c.getName(),JOptionPane.PLAIN_MESSAGE);
		return list.getSelectedValue();
	}
}
